package it.diego.projecttwo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.widget.Toast;

public class GestureActivity extends Activity {
	
	private GestureLibrary gLib;
	private static final String TAG = "com.android.gesture";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture);
        
        gLib = GestureLibraries.fromFile("/sdcard/gestures");
        if (!gLib.load()) {
            Toast.makeText(this, "Could not load /sdcard/gestures", Toast.LENGTH_SHORT).show();
            finish();
        }else{
        	Toast.makeText(this, "Caricato!!!", Toast.LENGTH_SHORT).show();
        	finish();
        }
        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(handleGestureListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gesture, menu);
        return true;
    }
    
    /**
	 * our gesture listener
	 */
	private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
		@Override
		public void onGesturePerformed(GestureOverlayView gestureView,
				Gesture gesture) {
 
			ArrayList predictions = gLib.recognize(gesture);
 
			// one prediction needed
			if (predictions.size() > 0) {
				Prediction prediction = (Prediction)predictions.get(0);
				// checking prediction
				if (prediction.score > 1.0) {
					// and action
					Toast.makeText(GestureActivity.this, prediction.name,
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
}
